import { areaList } from '@vant/area-data';
import toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import { addAddress, getAddress, updateAddress } from '../../server/address';
import { verification } from '../../utils/verification';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    show: false,
    name: "",
    phone: "",
    address: "",
    province: "",
    city: "",
    county: "",
    address: "",
    allAddress: "",
    error_name: "",
    is_default: 0,
    checked: false,
    is_error_name: false,
    error_address: "",
    is_error_address: false,
    error_phone: "",
    is_phone_name: false,
    error_city: "",
    is_error_city: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    if (options.id) {
      getAddress(options.id).then(res => {
        // console.log(res);
        this.setData({
          addressId: options.id,
          ...res,
          allAddress: res.province + '-' + res.city + '-' + res.county
        })
        wx.setNavigationBarTitle({
          title: '编辑地址'
        })
      })
    }
  },
  onShowCity() {
    this.setData({
      show: true
    })
  },
  clickConfirm(e) {
    // console.log(e.detail.values);
    this.setData({
      show: false,
      province: e.detail.values[0].name,
      city: e.detail.values[1].name,
      county: e.detail.values[2].name,
      allAddress: e.detail.values[0].name + '-' + e.detail.values[1].name + '-' + e.detail.values[2].name
    })
    this.checkcity()
  },
  checkcity() {
    if (verification.JudgmentNull(this.data.city)) {
      this.setData({
        error_city: "",
        is_error_city: true,

      })
    } else {
      this.setData({
        error_city: "地址不能为空",
        is_error_city: true,
      })
    }
  },
  clickCancel() {
    this.setData({
      show: false
    })
  },
  checkName() {
    // console.log(e.detail.value);
    // this.setData({
    //   name: e.detail.value
    // })
    if (verification.JudgmentNull(this.data.name)) {
      this.setData({
        error_name: "",
        is_error_name: true
      })
    } else {
      this.setData({
        error_name: "收货人姓名不能为空",
        is_error_name: false
      })

    }
  },
  checkPhone() {
    // console.log(e.detail.value);
    // this.setData({
    //   phone: e.detail.value
    // })
    if (verification.JudgmentNull(this.data.phone)) {
      // this.setData({
      //   phone_name: "",
      //   is_phone_name: true
      // })
      if (verification.phone(this.data.phone)) {
        this.setData({
          error_phone: "",
          is_phone_name: true
        })
      } else {
        this.setData({
          error_phone: "收货人手机号格式不正确",
          is_phone_name: false
        })
      }
    } else {
      this.setData({
        error_phone: "收货人手机号不能为空",
        is_phone_name: false
      })
    }
  },
  checkAddress() {
    if (verification.JudgmentNull(this.data.address)) {
      this.setData({
        error_address: "",
        is_error_address: true,
      })
    } else {
      this.setData({
        error_address: "详细地址不能为空",
        is_error_address: false,
      })
    }
  },
  onChange(e) {
    // console.log(e.detail);
    if (e.detail) {
      this.setData({
        checked: e.detail,
        is_default: 1
      })
    } else {
      this.setData({
        checked: e.detail,
        is_default: 0
      })
    }

  },
  toAddAddress() {
    this.checkcity()
    this.checkName()
    this.checkPhone()
    this.checkAddress()
    let { name, address, phone, province, city, county, is_default, is_error_name, is_error_address, is_phone_name, is_error_city } = this.data
    if (is_error_name && is_error_address && is_phone_name && is_error_city) {
      let data = {
        name, address, phone, province, city, county, is_default
      }
      if (this.data.addressId) {
        updateAddress(this.data.addressId,data).then(() => {
          toast({
            type: "success",
            message: "修改成功",
            onClose: () => {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        })
      } else {
        addAddress(data).then(res => {
          // console.log(res);
          toast({
            type: "success",
            message: "添加成功",
            onClose: () => {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        })
      }

    }
  }
})